<?php

namespace app\controllers;

use app\models\PsychologicalTestAnswers;
use app\models\AnswerOptions;
use app\models\Patients;

use lithium\security\Auth;
use lithium\action\DispatchException;

class PsychologicalTestAnswersController extends \lithium\action\Controller {

	public function index() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$psychologicalTestAnswers = PsychologicalTestAnswers::all();
		return compact('psychologicalTestAnswers');
	}

	public function view() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$psychologicalTestAnswer = PsychologicalTestAnswers::first($this->request->id);
		return compact('psychologicalTestAnswer');
	}

	public function add() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$psychologicalTestAnswer = PsychologicalTestAnswers::create();

		$patients = Patients::all(array(
			'order' => array('PatID' => 'ASC')
		));

		$answer_options = AnswerOptions::all(array(
			'return' => 'array',
			'order' => array('type' => 'ASC')
		));

		$anxietyDropdown = array();
		$depressionDropDown = array();

		foreach ($answer_options as $answer_option) {
			if ($answer_option['id'] == 8) {
				$anxietyDropdown[$answer_option['type']] = $answer_option['type'] . ' : ' . $answer_option['description'];
			}
			if ($answer_option['id'] == 9) {
				$depressionDropDown[$answer_option['type']] = $answer_option['type'] . ' : ' . $answer_option['description'];
			}
		}

		if (($this->request->data) && $psychologicalTestAnswer->save($this->request->data)) {
			return $this->redirect(array('PsychologicalTestAnswers::view', 'args' => array($psychologicalTestAnswer->id)));
		}
		return compact('psychologicalTestAnswer', 'patients', 'anxietyDropdown', 'depressionDropDown');
	}

	public function edit() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$psychologicalTestAnswer = PsychologicalTestAnswers::find($this->request->id);

		if (!$psychologicalTestAnswer) {
			return $this->redirect('PsychologicalTestAnswers::index');
		}
		if (($this->request->data) && $psychologicalTestAnswer->save($this->request->data)) {
			return $this->redirect(array('PsychologicalTestAnswers::view', 'args' => array($psychologicalTestAnswer->id)));
		}
		return compact('psychologicalTestAnswer');
	}

	public function delete() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		if (!$this->request->is('post') && !$this->request->is('delete')) {
			$msg = "PsychologicalTestAnswers::delete can only be called with http:post or http:delete.";
			throw new DispatchException($msg);
		}
		PsychologicalTestAnswers::find($this->request->id)->delete();
		return $this->redirect('PsychologicalTestAnswers::index');
	}
}

?>